package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.dto.BrandDTO;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
//@RefreshScope
public class BrandController {

//    @Value("${ip.leyou}")
//    private String ip;

    @Autowired
    private BrandService brandService;

    @GetMapping("/brand/page")
    public ResponseEntity<PageResult<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "page" ,defaultValue = "1")Integer page,
            @RequestParam(value = "rows" ,defaultValue = "5")Integer rows,
            @RequestParam(value = "key" ,required = false)String key,
            @RequestParam(value = "sortBy" ,required = false)String sortBy,
            @RequestParam(value = "desc" ,defaultValue = "false")Boolean desc
    ){
        PageResult<BrandDTO> result = brandService.queryBrandByPage(page, rows, key, sortBy, desc);
        return  ResponseEntity.ok(result);

    }

    @PostMapping("/brand")
    //请求参数的名字需要跟 后端的属性名字一致
    public ResponseEntity<Void>  saveBrand(BrandDTO brandDTO,
        @RequestParam(value = "cids")List<Long> cids){
        brandService.saveBrand(brandDTO,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping("/brand")//更新数据
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO
            ,@RequestParam(value = "cids")List<Long> cids){
        brandService.updateBrand(cids,brandDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @DeleteMapping("/brand")//删除数据
    public ResponseEntity<Void> deleteBrand(Long id){
        brandService.deleteBrand(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/brand/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable(value = "id")Long id){
//        log.info("========================="+ip);
        BrandDTO brandDTO = brandService.queryBrandById(id);
        return  ResponseEntity.ok(brandDTO);
    }

    @GetMapping("/brand/of/category")
    public ResponseEntity<List<BrandDTO>> findBrandsById(@RequestParam(value = "id")Long id){
        List<BrandDTO> list =  brandService.findBrandsById(id);
        return ResponseEntity.ok(list);
    }


}
